#include <iostream>
#include "GL/glut.h"
#include "Planning2D.h"

using namespace std;

bool init(int argc, char *argv[])
{
	return Planning2D::getInstance()->init(argc, argv);	
}

void display()
{	
	glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT);

	Planning2D::getInstance()->render();

	glutSwapBuffers();
}

void keyboard(unsigned char key, int x, int y)
{
	switch(key)
	{
	// Spacebar
	case 32 :
	Planning2D::getInstance()->addSamples();
	glutPostRedisplay();
	break;

	case 'a' :
	Planning2D::getInstance()->increaseNumSamplesPerIteration();
	break;
		
	case 'z' :
	Planning2D::getInstance()->decreaseNumSamplesPerIteration();
	break;

	case 't' :
	Planning2D::getInstance()->printTimings();
	break;
	}
}

void reshape(GLsizei width, GLsizei height) {  
	int left, right, bottom, top;
	Planning2D::getInstance()->getBoundingBox(left, right, top, bottom);
	glLoadIdentity();
	gluOrtho2D(left - 2, right + 2, bottom - 2, top + 2);
}

int main(int argc, char *argv[])
{
	if (!init(argc, argv))
		return 1;

	const int windowWidth = 800;
	int left, right, bottom, top;
	Planning2D::getInstance()->getBoundingBox(left, right, top, bottom);
	float width = right - left + 4;
	float height = top - bottom + 4;
	int windowHeight = height / width * windowWidth;

	glutInit(&argc, argv);
	glutInitWindowPosition(50, 50);
	glutInitWindowSize(windowWidth, windowHeight);
	glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
	glutCreateWindow("HW2 - chpark");
	glutReshapeFunc(reshape); 
	glutKeyboardFunc(keyboard);
	glutDisplayFunc(display);

	cout << "\nPress spacebar to add samples\n";

	glutMainLoop();

	return 0;
}